<?php

namespace App\Services;

use App\Models\DossierCombination;
use App\Models\DossierTemplate;
use Illuminate\Database\Eloquent\Model;

/**
 * 案卷构成模板服务类
 * Class DossierTemplateService
 * @package App\Services
 */
class DossierTemplateService extends BaseService
{
    /**
     * 新增一个案卷构成规范
     */
    public function saveData($params)
    {
        if ($params['id']) {
            $dossierTemplate = DossierTemplate::query()->find($params['id']);
            $content = '编辑一个案卷构成规范';
        } else {
            $dossierTemplate = new DossierTemplate();
            $content = '新增一个案卷构成规范';
        }
        $dossierTemplate->title  = format_str($params['title']);
        $dossierTemplate->thumb  = $params['thumb'] ?? ''; //缩略图
        $dossierTemplate->type   = $params['type']; //1封面 2备考表 3案卷 4卷内 5全引
        $dossierTemplate->url    = $params['url'] ?? ''; //文件地
        $dossierTemplate->origin_name = $params['origin_name'] ?? ''; //原文件名

        $result = $dossierTemplate->save();

        CommonService::addActionLog($content, "数据ID：{$dossierTemplate->id}");

        return $result;
    }

    /**
     * 获取一条案卷构成模板信息
     */
    public function getInfo($id)
    {
        return DossierTemplate::query()->where('id', $id)->first();
    }

    /**
     * 目录编辑
     */
    public function updateData($id, $params)
    {
        $dossierTemplate = DossierTemplate::query()->where('id', $id)->first();
        $before = $dossierTemplate->title;
        $dossierTemplate->title  = format_str($params['title']);
        $dossierTemplate->thumb  = $params['thumb']; //缩略图
        $dossierTemplate->type   = $params['type']; //1封面 2备考表 3案卷 4卷内 5全引
        $dossierTemplate->url    = $params['url']; //文件地址
        $dossierTemplate->sort   = $params['sort'] ?? 10;
        $dossierTemplate->remark = format_str($params['remark']);

        CommonService::addActionLog('编辑案卷构成规范', "把{$before}修改为{$dossierTemplate->title}");

        return $dossierTemplate->save();
    }

    /**
     * 获取 ids
     * @param int $type (1-封面, 2-备考表, 3-案卷目录(书名), 4-卷内目录(书目录), 5-全引目录)
     */
    public static function getIdsByType($type = 0, $keyword = null)
    {
        $ids = DossierTemplate::query()
            ->where('type', $type)
            ->when($keyword, function ($query, $keyword) {
                return $query->where('title', 'like', "%{$keyword}%");
            })
            ->pluck('id');

        return $ids->isNotEmpty() ? $ids->toArray() : [];
    }

    /**
     * 案卷构成模板
     */
    public function getList($request, $perPage = 0)
    {
        $thisQuery = DossierTemplate::query();
        if ($request->filled('keyword')) {
            $keyword = $request->input('keyword');
            $thisQuery->where('title', 'like', "%{$keyword}%")->orWhere('id', '=', $keyword);
        }
        $list = $thisQuery->orderByDesc('id')->paginate($perPage);

        if ($list->isEmpty()) return [];

        $list = $list->toArray();
        foreach ($list['data'] as &$val) {
            if ($val['url']) { //构造一个文件下载地址，页面点击，直接下载文件
                $val['url'] = $this->host . 'api/dossier_templates/download?file_url=' . $val['url'] . '&origin_name=' . $val['origin_name'];
            }
            $val['type_text'] = DossierTemplate::TYPE[$val['type']];
        }
        return $this->removeNoUsedPageParams($list);
    }

    /**
     * 获取回收站数据列表
     */
    public function getRecycles($request): array
    {
        $thisQuery = DossierTemplate::onlyTrashed();

        if ($request->filled('keyword')) { //判断参数是否存在并且不为空
            $keyword = $request->input('keyword');
            $thisQuery->where('title', 'like', "%{$keyword}%"); //由于不同的表字段不同，所以单独拿出来
        }
        $list = $this->formatRecycleData($request, $thisQuery);
        if ($list) {
            foreach ($list['data'] as &$val) { //将案卷构成模板的类型名加到后面，方便区分
                $typeText = DossierTemplate::TYPE[$val['type']];
                $val['title'] = $val['title'] . '(' . $typeText . ')';
            }
        }

        return $list;
    }

}
